<?php

class GetController extends Controller
{
    public function actionIndex()
    {
        $this->render('index');
    }

    public function actionTemplates()
    {
        $response = array();
        $templates = array();
        if(isset($_POST['templates'])){
            foreach ($_POST['templates'] as $key => $value){
                $templates[$key] = file_get_contents($value);
            }
            $response['templates'] = $templates;
        }
        echo CJSON::encode($response);
    }

    public function actionDuplicateEmail()
    {
        if(isset($_POST['email'])){
            $data = array();
            $data['email'] = $_POST['email'];
            $account = Account::model()->findByAttributes(array('email'=>$_POST['email']));
            if($account == null){
                $data['duplicateEmail'] = false;
            }
            else{
                $data['duplicateEmail'] = true;
            }
            echo CJSON::encode($data);
        }
    }
    /* Get Category 2 Business
     * GET Link : $this->createUrl("get/GetCat2") , $cat1_id = var category 1 id
     * Function : GetController::GetCat2($cat1_id);
     */

    public function actionGetCat2 () {
        $cat1_id = $_GET['cat1_id'];
        $key = md5(trim("Cat2::model()->findAll(cat1_id = $cat1_id)"));
        if ( Yii::app()->cache->get($key) == false ) {
            $cat2 = Cat2::model()->findAll("cat1_id = $cat1_id");
            Yii::app()->cache->get($key);
            echo CJSON::encode($cat2);
        } else {
            echo CJSON::encode(Yii::app()->cache->get($key));
        }

    }

    public static function GetCat2 ($cat1_id) {
        $key = md5(trim("Cat2::model()->findAll(cat1_id = $cat1_id)"));
        if ( Yii::app()->cache->get($key) == false ) {
            $cat2 = Cat2::model()->findAll("cat1_id = $cat1_id");
            Yii::app()->cache->set($key,$cat2);
            return $cat2;
        } else {
            return Yii::app()->cache->get($key);
        }
    }

    /* Get Province
     * Get Link : $this->createUrl("get/GetProvince")
     * Function : GetController::GetProvince();
     */

    public function actionsGetProvince() {
        $cache = Yii::app()->cache->get("province");
        if ( $cache == false ) {
            $prov = Province::model()->findAll();
            Yii::app()->cache->set("province",$prov);
            echo CJSON::encode($prov);
        } else {
            echo CJSON::encode($cache);
        }
    }

    public static function GetProvince() {
        $cache = Yii::app()->cache->get("province");
        if ( $cache == false ) {
            $prov = Province::model()->findAll();
            Yii::app()->cache->set("province",$prov);
            return $prov;
        } else {
            return $cache;
        }

    }

    /*  Get Amphur Function & Get
     *  Get Link :  $this->createUrl("get/GetAmphur") , $prov_id = var Province id
     *  Function : GetController::GetAmphur($prov_id);
     */
    public function actionGetAmphur() {
        $prov_id = $_GET['prov_id'];
        $key = md5(trim("Amphur::model()->findAll( province_id = '$prov_id')"));
        if ( Yii::app()->cache->get($key) == false ) {
            $amphur = Amphur::model()->findAll("province_id = $prov_id");
            Yii::app()->cache->set($key,$amphur);
            echo CJSON::encode($amphur);
        } else {
            return CJSON::encode(Yii::app()->cache->get($key));
        }
    }

    /*
     * get place from attribute object (JSON)
     */
    public static function actionGetPlace()
    {
        $response = array();
        $response['success'] = false;
        if(isset($_POST['options'])){
            $attributes = array();
            if(isset($_POST['options'])){
                $options = $_POST['options'];

                // test options
                $options['lat1'] = 0;
                $options['lat2'] = 200;
                $options['lng1'] = 0;
                $options['lng2'] = 200;
                // end test options

                $criteria = new CDbCriteria();
                $criteria->condition = "(lat > :lat1 AND lng > :lng1) AND (lat < :lat2 AND lng < :lng2)";
                $criteria->params = array(
                    ':lat1' => $options['lat1'],
                    ':lng1' => $options['lng1'],
                    ':lat2' => $options['lat2'],
                    ':lng2' => $options['lng2'],
                );
            }
            $response['places'] = Place::model()->findAll($criteria);
            $response['success'] = true;
        }
        echo CJSON::encode($response);
    }

    public function actionIsDuplicateLink()
    {
        $response = array();
        $response['success'] = false;
        if(isset($_POST['link'])) {
            $duplicateLinkForm = new DuplicateLinkForm();
            $duplicateLinkForm->link = $_POST['link'];
            $duplicateLinkForm->check();
            $response['duplicateLink'] = $duplicateLinkForm;
        }
        $response['success'] = true;
        echo CJSON::encode($response);
    }

    /*  Get Amphur Function & Get
    *  Get Link :  $this->createUrl("get/GetAmphur") , $prov_id = var Province id
    *  Function : GetController::GetAmphur($prov_id);
    */

    /*  Get District Function & Get
     *  Get Link :  $this->createUrl("get/GetDistrict") , $prov_id = var Province id , $amphur_id = var Amphur id
     *  Function : GetController::GetDistrict($prov_id,$amphur_id);
     */
    public function actionGetDistrict() {
        $prov_id = $_GET['prov_id'];
        $amphur_id = $_GET['amphur_id'];
        $key = md5(trim("District::model()->findAll(province_id = '$prov_id' and amphur_id = '$amphur_id')"));
        if ( Yii::app()->cache->get($key) == false ) {
            $district = District::model()->findAll("province_id = $prov_id and amphur_id = $amphur_id");
            Yii::app()->cache->set($key,$district);
            echo CJSON::encode($district);
        } else {
            echo CJSON::encode(Yii::app()->cache->get($key));
        }
    }

    public static function GetDistrict($prov_id,$amphur_id) {
        $key = md5(trim("District::model()->findAll(province_id = '$prov_id' and amphur_id = '$amphur_id')"));
        if ( Yii::app()->cache->get($key) == false ) {
            $district = District::model()->findAll("province_id = $prov_id and amphur_id = $amphur_id");
            Yii::app()->cache->set($key,$district);
            return $district;
        } else {
            return Yii::app()->cache->get($key);
        }
    }

}